<template>
  <div class="college">
    <!-- 按钮 -->
    <div class="btns">
      <el-button type="primary" size="small" @click="toSaveHandler">新增</el-button>
    </div>
    <!-- 按钮 -->
    <!-- 表格 -->
    <el-table size="small" :data="collegeData.list" v-loading="loading">
      <el-table-column label="序号" type="index" :index="1"></el-table-column>
      <el-table-column label="名称" prop="name" width="200"></el-table-column>
      <el-table-column label="介绍" prop="description" ></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="toDeleteHandler(scope.row)">删除</el-button>
          <el-button type="text" size="mini" @click="toEditHandler(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格 -->
    <!-- 分页 -->
    <el-pagination background layout="prev, pager, next" small
      :hide-on-single-page="true"
      :total="collegeData.total" 
      :page-size="collegeData.pageSize" 
      :current-page="collegeData.page"
      @current-change="currentChangeHandler">
    </el-pagination>
    <!-- 分页 -->
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="visible" width="50%" @close="clearValidate">
      <div>
        <el-form :model="form" ref="collegeForm" :rules="rules" label-width="80px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="介绍" prop="description">
            <el-input type="textarea" v-model="form.description"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取消</el-button>
        <el-button size="small" type="primary" @click="submitHandler">确定</el-button>
      </span>
    </el-dialog>
    <!-- 模态框 -->
  </div>
</template>
<script>
import {get,post} from '@/utils/request'
import _ from 'lodash'
export default {
  data(){
    return {
      visible:false,
      loading:false,
      title:"新增学院信息",
      param:{
        page:1,
        pageSize:15
      },
      collegeData:{
        list:[]
      },
      form:{},
      rules:{
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入简介', trigger: 'blur' }
        ]
      }
    }
  },
  created(){
    this.loadColleges();
  },
  methods:{
    submitHandler(){
      console.log(this.$refs['collegeForm']);
      this.$refs['collegeForm'].validate((valid) => {
        if (valid) {
          let url = '/college/saveOrUpdate'
          post(url,this.form).then(response => {
            this.$message({type:"success",message:response.message})
            this.loadColleges();
            this.visible = false;
          })
        } else {
          return false;
        }
      });
    },
    // 去保存
    toSaveHandler(){
      this.title = "新增学院信息";
      this.form = {};
      this.visible = true;
    },
    // 去修改
    toEditHandler(row){
      this.title = "修改学院信息"
      this.form = _.clone(row);
      this.visible = true;
    },
    // 去删除
    toDeleteHandler(row){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = '/college/deleteById'
        get(url,{id:row.id}).then(response =>{
          this.$message({ type: 'success', message: '删除成功!' });
          this.loadColleges();
        })
      })
    },
    // 查询
    loadColleges(){
      this.loading = true;
      let url = "/college/pageQuery"
      get(url,this.param).then(response => {
        this.collegeData = response.data;
        this.loading = false;
      })
    },
    // 当前页发生改变
    currentChangeHandler(page){
      this.param.page = page;
      this.loadColleges();
    },
    // 重置表单校验
    clearValidate() {
      this.$refs['collegeForm'].clearValidate();
    }
  }
}
</script>